
// enb by qq3506796429_________________________________
//                __________________
//            ____|Edited by Ethmods|____
//           |Code from SADX by XMakarusX| 
//
//                  Youtube Channel:
//       https://m.youtube.com/channel/UCqscbzXtZxyYdTeFWowXSRA
//
//                   Facebook Page:
//       http://www.facebook.com/Ethmods-2264724780438107
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ENBSeries effect file
// visit http://enbdev.com for updates
// Copyright (c) 2007-2020 Boris Vorontsov
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



//+++++++++++++++++++++++++++++
//internal parameters, can be modified
//+++++++++++++++++++++++++++++

//Lighting
float Lighting_Brightness <string UIName="Lighting - Brightness";string UIWidget="Spinner";float UIMin=0.0;float UIMax=10.0;> = {1.20};
float Lighting_Saturate <string UIName="Lighting - Saturate";string UIWidget="Spinner";float UIMin=0.0;float UIMax=5.0;> = {1.20};
float Lighting_Contrast <string UIName="Lighting - Contrast";string UIWidget="Spinner";float UIMin=0.0;float UIMax=5.0;> = {1.30};


//Shadows
float Shadow_Brightness <string UIName="Shadow - Brightness";string UIWidget="Spinner";float UIMin=0.0;float UIMax=2.0;> = {0.65};
float Shadow_Saturate <string UIName="Shadow - Saturate";string UIWidget="Spinner";float UIMin=0.0;float UIMax=5.0;> = {0.80};
float Shadow_Contrast <string UIName="Shadow - Contrast";string UIWidget="Spinner";float UIMin=0.0;float UIMax=5.0;> = {1.80};


//Extras
float Peds_SideLighting <string UIName="Peds - Fresnel Lighting";string UIWidget="Spinner";float UIMin=0.0;float UIMax=10.0;> = {2.00};
float Lighting_Level <string UIName="Cars - Shadow";string UIWidget="Spinner";float UIMin=0.0;float UIMax=10.0;> = {0.60};
float PBR_Brightness <string UIName="PBR - Brightness";string UIWidget="Spinner";float UIMin=0.0;float UIMax=10.0;> = {2.00};

//Sky
float	Sky_Saturate <string UIName="Sky - Saturate";string UIWidget="Spinner";float UIMin=0.0;float UIMax=6.0;> = {1.30};
float	Sky_Contrast <string UIName="Sky - Contrast";string UIWidget="Spinner";float UIMin=0.0;float UIMax=6.0;> = {1.35};
float	Sky_Brightness <string UIName="Sky - Brightness";string UIWidget="Spinner";float UIMin=0.0;float UIMax=6.0;> = {1.00};

float Stars_Brightness <string UIName="Stars - Brightness";string UIWidget="Spinner";float UIMin=0.0;float UIMax=5.0;> = {1.00};
float starscover <string UIName="Stars - Size";string UIWidget="Spinner";float UIMin=0.0;float UIMax=0.20;> = {0.05};

float vb_t <string UIName="VolumeBox_top";string UIWidget="Spinner";>;
float vb_b <string UIName="VolumeBox_bottom";string UIWidget="Spinner";>;
float min_sl <string UIName="MinStepLength";string UIWidget="Spinner";> = { 1.0 };
float max_sl <string UIName="MaxStepLength";string UIWidget="Spinner";> = { 40.0 };
float step_mul <string UIName="StepLengthMultiplyer";string UIWidget="Spinner";> = { 3.0 };
float maxStep <string UIName="MaxStep";string UIWidget="Spinner";> = { 100.0 };
float dCut <string UIName="DensityCut";string UIWidget="Spinner";> = { 0.0 };
float tCut <string UIName="TransparencyCut";string UIWidget="Spinner";> = { 0.01 };

float body_top <string UIName="BodyTop";string UIWidget="Spinner";> = { 0.8 };
float body_mid <string UIName="BodyMiddle";string UIWidget="Spinner";> = { 0.4 };
float body_bot <string UIName="BodyBottom";string UIWidget="Spinner";> = { 0.2 };
float body_thickness <string UIName="BodyThickness";string UIWidget="Spinner";> = { 0.0 };

float grow <string UIName="DensityIncrease";string UIWidget="Spinner";> = { 1.0 };
float grow_c <string UIName="DensityIncrease_cloudy";string UIWidget="Spinner";> = { 1.0 };

float soft_top <string UIName="DensitySoftnessTop";string UIWidget="Spinner";> = { 40.0 };
float soft_bot <string UIName="DensitySoftnessBottom";string UIWidget="Spinner";> = { 40.0 };
float soft_bot_c <string UIName="DensitySoftnessBottom_cloudy";string UIWidget="Spinner";> = { 40.0 };

float noise_densA <string UIName="ChunkDensityA";string UIWidget="Spinner";> = { 0.43 };
float noise_densB <string UIName="ChunkDensityB";string UIWidget="Spinner";> = { 1.02 };
float noise_densC <string UIName="DetailDensityC";string UIWidget="Spinner";> = { 0.30 };
float noise_densD <string UIName="DetailDensityD";string UIWidget="Spinner";> = { 0.87 };
float noise_densE <string UIName="DetailDensityE";string UIWidget="Spinner";> = { 0.87 };
float noiseSizeA <string UIName="ChunkSizeA";string UIWidget="Spinner";> = { 48.6 };
float noiseSizeB <string UIName="ChunkSizeB";string UIWidget="Spinner";> = { 7.75 };
float noiseSizeC <string UIName="DetailSizeC";string UIWidget="Spinner";> = { 0.33 };
float noiseSizeD <string UIName="DetailSizeD";string UIWidget="Spinner";> = { 0.54 };
float noiseSizeE <string UIName="DetailSizeE";string UIWidget="Spinner";> = { 0.54 };

float temp1 <string UIName="temp1";string UIWidget="Spinner";> = { 1.0 };
float temp2 <string UIName="temp2";string UIWidget="Spinner";> = { 1.0 };

float3 cloud_shift <string UIName="shift";string UIWidget="Vector";> = { 0.0, 0.0, 0.0 };

float curvy_scale <string UIName="curvy_scale";string UIWidget="Spinner";> = { 2.5 };
float curvy_ang <string UIName="curvy_ang";string UIWidget="Spinner";> = { 2.5 };
float curvy_strength <string UIName="curvy_strength";string UIWidget="Spinner";> = { 2.5 };

float BeerFactor <string UIName="BeerFactor";string UIWidget="Spinner";> = { 0.0 };
float EffectFactor <string UIName="EffectFactor";string UIWidget="Spinner";> = { 2.5 };
float CloudSoftness <string UIName="CloudSoftness";string UIWidget="Spinner";> = { 0.0 };

float scattering <string UIName="ScatteringRange";string UIWidget="Spinner";> = { 0.0 };
float scatteringStrength <string UIName="ScatteringStrength";string UIWidget="Spinner";> = { 0.0 };
float moonlight_strength <string UIName="MoonStrength";string UIWidget="Spinner";> = { 0.0 };

float CloudContrast <string UIName="CloudContrast";string UIWidget="Spinner";> = { 0.0 };
float cloudBrightnessMultiply <string UIName="CloudBrightnessMultiply";string UIWidget="Spinner";> = { 2.5 };

float ShadowStepLength <string UIName="ShadowStepLength";string UIWidget="Spinner";> = { 10.0 };
float DetailShadowStepLength <string UIName="DetailShadowStepLength";string UIWidget="Spinner";> = { 10.0 };
float ShadowDetail <string UIName="DetailShadowStrength";string UIWidget="Spinner";> = { 1.0 };
float shadowExpand <string UIName="ShadowExpand";string UIWidget="Spinner";> = { 0 };
float shadowStrength <string UIName="ShadowStrength";string UIWidget="Spinner";> = { 1 };
float shadowContrast <string UIName="ShadowContrast";string UIWidget="Spinner";> = { 1 };
float shadowSoftness <string UIName="ShadowSoftness";string UIWidget="Spinner";> = { 0 };

float fogEffect <string UIName="FogEffect";string UIWidget="Spinner";> = { 0 };
float fogEffect_c <string UIName="FogEffect_cloudy";string UIWidget="Spinner";> = { 0 };

float alpha_curve <string UIName="AlphaCurve";string UIWidget="Spinner";> = { 2.5 };
float fade_s <string UIName="FadeStart";string UIWidget="Spinner";> = { 0.0 };
float fade_e <string UIName="FadeEnd";string UIWidget="Spinner";> = { 2.5 };

float Atomesphere_Distance <string UIName="AtomesphereDistance";string UIWidget="Spinner";> = { 1000.0 };
float Atomesphere_Smoothness <string UIName="AtomesphereSmoothness";string UIWidget="Spinner";> = { 1000.0 };
float3 Atomesphere <string UIName="Atomesphere";string UIWidget="Vector";> = { 0.0, 0.0, 0.0 };

float speed <string UIName="Speed";string UIWidget="Spinner";> = { 0.0 };
float3 nA_move <string UIName="NoiseA_Direction";string UIWidget="Vector";> = { 0.0, 0.0, 0.0 };
float3 nB_move <string UIName="NoiseB_Direction";string UIWidget="Vector";> = { 0.0, 0.0, 0.0 };
float3 nC_move <string UIName="NoiseC_Direction";string UIWidget="Vector";> = { 0.0, 0.0, 0.0 };
float3 nD_move <string UIName="NoiseD_Direction";string UIWidget="Vector";> = { 0.0, 0.0, 0.0 };

float3 CBC_D <string UIName="Cloud Dark Color Day";string UIWidget="Color";> = { 0.0, 0.0, 0.0 };
float3 CBC_N <string UIName="Cloud Dark Color Night";string UIWidget="Color";> = { 0.0, 0.0, 0.0 };
float3 CBC_S <string UIName="Cloud Dark Color SunSet";string UIWidget="Color";> = { 0.0, 0.0, 0.0 };
float3 CBC_CD <string UIName="Cloud Dark Color Cloudy Day";string UIWidget="Color";> = { 0.0, 0.0, 0.0 };
float3 CBC_CN <string UIName="Cloud Dark Color Cloudy Night";string UIWidget="Color";> = { 0.0, 0.0, 0.0 };

float3 CTC_D <string UIName="Cloud Bright Color Day";string UIWidget="Color";> = { 0.0, 0.0, 0.0 };
float3 CTC_N <string UIName="Cloud Bright Color Night";string UIWidget="Color";> = { 0.0, 0.0, 0.0 };
float3 CTC_S <string UIName="Cloud Bright Color SunSet";string UIWidget="Color";> = { 0.0, 0.0, 0.0 };
float3 CTC_CD <string UIName="Cloud Bright Color Cloudy Day";string UIWidget="Color";> = { 0.0, 0.0, 0.0 };
float3 CTC_CN <string UIName="Cloud Bright Color Cloudy Night";string UIWidget="Color";> = { 0.0, 0.0, 0.0 };

float grow_h <string UIName="fatnessH";string UIWidget="Spinner";> = { 1.0 };
float softness_h <string UIName="softnessH";string UIWidget="Spinner";> = { 40.0 };
float scatteringStrength_h <string UIName="ScatteringStrength_h";string UIWidget="Spinner";> = { 0.0 };
float H_scaleA <string UIName="HscaleA";string UIWidget="Spinner";> = { 40.0 };
float H_scaleB <string UIName="HscaleB";string UIWidget="Spinner";> = { 40.0 };
float curvy_scale_h <string UIName="curvy_scale_h";string UIWidget="Spinner";> = { 2.5 };
float curvy_ang_h <string UIName="curvy_ang_h";string UIWidget="Spinner";> = { 2.5 };
float curvy_strength_h <string UIName="curvy_strength_h";string UIWidget="Spinner";> = { 2.5 };
float H_noise_densA <string UIName="HdensA";string UIWidget="Spinner";> = { 40.0 };
float H_noise_densB <string UIName="HdensB";string UIWidget="Spinner";> = { 40.0 };
float3 H_nA_move <string UIName="H_NoiseA_Direction";string UIWidget="Vector";> = { 0.0, 0.0, 0.0 };
float3 H_nB_move <string UIName="H_NoiseB_Direction";string UIWidget="Vector";> = { 0.0, 0.0, 0.0 };



//Rain
bool Puddles_alltime <string UIName="Puddles_alltime";	> = {true};
float Puddles_alltime_coverage <string UIName="Puddles_alltime - coverage";string UIWidget="Spinner";float UIMin=0.0;float UIMax=2.0;> = {0.65};



//+++++++++++++++++++++++++++++
//external parameters, do not modify
//+++++++++++++++++++++++++++++
//keyboard controlled temporary variables (in some versions exists in the config file). Press and hold key 1,2,3...8 together with PageUp or PageDown to modify. By default all set to 1.0
float4	tempF1; //0,1,2,3
float4	tempF2; //5,6,7,8
float4	tempF3; //9,0
//x=Width, y=1/Width, z=ScreenScaleY, w=1/ScreenScaleY
float4	ScreenSize;
//x=generic timer in range 0..1, period of 16777216 ms (4.6 hours), w=frame time elapsed (in seconds)
float4	Timer;
//changes in range 0..1, 0 means that night time, 1 - day time
float	ENightDayFactor;
//changes 0 or 1. 0 means that exterior, 1 - interior
float	EInteriorFactor;
//.x - current weather index, .y - incoming weather index, .z - weather transition, .w - time of the day in 24 standart hours. Weather index is value from _weatherlist.ini, for example WEATHER002 means index==2, but index==0 means that weather not captured.
float4	WeatherAndTime;
//adaptation delta time for focusing
float	FadeFactor;
//fov in degrees
float	FieldOfView;
//time in 0..24 format
float	GameTime;
//sun direction in world space
float4	SunDirection; //.w - 1 when sun is set
//constants set in enbhelper.dll, can be anything captured from game
float4	CustomShaderConstants1[8];

//transposed transform matrix view*projection and inverse of it
float4	MatrixVP[4];
float4	MatrixInverseVP[4];
float4	MatrixVPRotation[4];
float4	MatrixInverseVPRotation[4];
float4	MatrixView[4];
float4	MatrixInverseView[4];
float4	CameraPosition;


float4x4	MatrixWVP;
float4x4	MatrixWVPInverse;
float4x4	MatrixWorld;
float4x4	MatrixProj;
float4	FogParam; //x - nearclip, y - farclip, z - fog start, w - fog end
float4	FogFarColor;


//textures
texture2D texEnv;
texture2D texOriginal;
texture2D texColor;
texture2D texDepth;
texture2D texNoise;
texture2D texShadow;
texture2D texPalette;
texture2D texFocus;
texture2D texCurr; 
texture2D texPrev; 
texture2D texNormal;
texture2D texStars < string ResourceName="stars.png"; >;
texture2D texRain < string ResourceName="Rain.png"; >;
texture2D texMoon < string ResourceName="Moon.png"; >;
texture2D texCd < string ResourceName="NoiseCd.png"; >;
texture2D texPd < string ResourceName="NoisePd.png"; >;
texture2D texRipples < string ResourceName="Ripples.png"; >;
texture2D texAlpha < string ResourceName="RipplesAlpha.png"; >;
texture2D texAlpha2 < string ResourceName="RipplesAlpha2.png"; >;
texture2D texwrl < string ResourceName="NoisePr.png"; >;
texture2D texltn < string ResourceName="lightning.png"; >;
texture2D texltn2 < string ResourceName="lightning2.png"; >;

sampler2D SamplerLightning2 = sampler_state
{
	Texture = <texltn2>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
    AddressU = Clamp;
    AddressV = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};


sampler2D SamplerLightning = sampler_state
{
	Texture = <texltn>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
    AddressU = Clamp;
    AddressV = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

sampler2D SamplerEnv = sampler_state
{
	Texture   = <texEnv>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
	AddressU  = Mirror;
	AddressV  = Mirror;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

sampler2D SamplerOriginal = sampler_state
{
	Texture   = <texOriginal>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;//NONE;
	AddressU  = Clamp;
	AddressV  = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

sampler2D SamplerWrl = sampler_state
{
	Texture = <texwrl>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
	AddressU = Wrap;
	AddressV = Wrap;
	AddressW = Wrap;
	SRGBTexture=TRUE;
	MaxMipLevel=1;
	MipMapLodBias=0;
};

sampler2D SamplerWrp = sampler_state
{
	Texture = <texRipples>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
	AddressU = Wrap;
	AddressV = Wrap;
	AddressW = Wrap;
	SRGBTexture=TRUE;
	MaxMipLevel=1;
	MipMapLodBias=0;
};

sampler2D SamplerAlpha = sampler_state
{
	Texture = <texAlpha>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
	AddressU = Wrap;
	AddressV = Wrap;
	AddressW = Wrap;
	SRGBTexture=TRUE;
	MaxMipLevel=1;
	MipMapLodBias=0;
};

sampler2D SamplerAlpha2 = sampler_state
{
	Texture = <texAlpha2>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
	AddressU = Wrap;
	AddressV = Wrap;
	AddressW = Wrap;
	SRGBTexture=TRUE;
	MaxMipLevel=1;
	MipMapLodBias=0;
};

sampler2D SamplerNormal = sampler_state
{
	Texture   = <texNormal>;
	MinFilter = Anisotropic;
	MagFilter = Anisotropic;
	MipFilter = LINEAR;
	AddressU  = Wrap;
	AddressV  = Wrap;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

sampler2D SamplerColor = sampler_state
{
	Texture   = <texColor>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = NONE;
	AddressU  = Clamp;
	AddressV  = Clamp;
	SRGBTexture=TRUE;
	MaxMipLevel=2;
	MipMapLodBias=0;
};

sampler2D SamplerDepth = sampler_state
{
	Texture   = <texDepth>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
	AddressU = Wrap;
	AddressV = Wrap;
	AddressW = Wrap;
	SRGBTexture=TRUE;
	MaxMipLevel=1;
	MipMapLodBias=0;
};

sampler2D SamplerNoise = sampler_state
{
	Texture   = <texNoise>;
	MinFilter = POINT;
	MagFilter = POINT;
	MipFilter = NONE;
	AddressU  = Wrap;
	AddressV  = Wrap;
	SRGBTexture=FALSE;
	MaxMipLevel=2;
	MipMapLodBias=0;
};

sampler2D SamplerPalette = sampler_state
{
	Texture   = <texPalette>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = NONE;
	AddressU  = Clamp;
	AddressV  = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};


sampler2D SamplerCurr = sampler_state
{
	Texture   = <texCurr>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;//NONE;
	AddressU  = Clamp;
	AddressV  = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};


sampler2D SamplerPrev = sampler_state
{
	Texture   = <texPrev>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = NONE;
	AddressU  = Clamp;
	AddressV  = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

sampler2D SamplerFocus = sampler_state
{
	Texture   = <texFocus>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = NONE;
	AddressU  = Clamp;
	AddressV  = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

sampler2D SamplerStars = sampler_state
{
   Texture = <texStars>;
   MinFilter = LINEAR;
   MagFilter = LINEAR;
   MipFilter = NONE;
   AddressU = WRAP;
   AddressV = WRAP;
   SRGBTexture=FALSE;
   MaxMipLevel=0;
   MipMapLodBias=0;
}; 

sampler2D SamplerRain = sampler_state
{
   Texture = <texRain>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
	AddressU = Wrap;
	AddressV = Wrap;
	AddressW = Wrap;
	SRGBTexture=TRUE;
	MaxMipLevel=0;
	MipMapLodBias=0;
}; 

sampler2D SamplerMoon = sampler_state
{
   Texture = <texMoon>;
   MinFilter = LINEAR;
   MagFilter = LINEAR;
   MipFilter = NONE;
   AddressU = Clamp;
   AddressV = Clamp;
   SRGBTexture=FALSE;
   MaxMipLevel=0;
   MipMapLodBias=0;
};

sampler2D SamplerClouds = sampler_state
{
   Texture   = <texCd>;
   MinFilter = LINEAR;
   MagFilter = LINEAR;
   MipFilter = LINEAR;
   AddressU  = Wrap;
   AddressV  = Wrap;
   AddressW  = Wrap;   
   SRGBTexture=FALSE;
   MaxMipLevel=0;
   MipMapLodBias=0;
};

sampler2D SamplerPuddle = sampler_state
{
   Texture   = <texPd>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
	AddressU = Wrap;
	AddressV = Wrap;
	AddressW = Wrap;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

sampler2D SamplerShadow = sampler_state
{
	Texture   = <texShadow>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = NONE;
	AddressU  = Clamp;
	AddressV  = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

struct VS_OUTPUT_POST
{
	float4 vpos  : POSITION;
	float2 txcoord : TEXCOORD0;
};

struct VS_INPUT_POST
{
	float3 pos  : POSITION;
	float2 txcoord : TEXCOORD0;
};



//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
VS_OUTPUT_POST VS_PostProcess(VS_INPUT_POST IN)
{
	VS_OUTPUT_POST OUT;
	float4 pos=float4(IN.pos.x,IN.pos.y,IN.pos.z,1.0);
	OUT.vpos=pos;
	OUT.txcoord.xy=IN.txcoord.xy;
	return OUT;
}

float linearlizeDepth(float nonlinearDepth)
{
float2 dofProj = float2(0.0509804, 3098.0392);
float2 dofDist = float2(0.0, 0.0509804);
float4 depth = nonlinearDepth;
       depth.y = -dofProj.x + dofProj.y;
       depth.y = 1.0/depth.y;
       depth.z = depth.y * dofProj.y;
       depth.z = depth.z * -dofProj.x;
       depth.x = dofProj.y * -depth.y + depth.x;
       depth.x = 1.0/depth.x;
       depth.y = depth.z * depth.x;
       depth.x = depth.z * depth.x - dofDist.y;
       depth.x+= dofDist.x * -0.5;
       depth.x = max(depth.x, 0.0);
return depth.x;
}

float CL(in float3 c)
{
	return (c.r * 0.2126 + c.g * 0.7152 + c.b * 0.0722)*1.0;
}

float4 wpd0(in float2 cd, in float d)
{	
float4 tv;
       tv.xy = cd.xy*2.0-1.0;
       tv.y = -tv.y;
       tv.z = d;
       tv.w = 1.0;
float4 wp;
       wp.x = dot(tv, MatrixInverseVPRotation[0]);
       wp.y = dot(tv, MatrixInverseVPRotation[1]);
       wp.z = dot(tv, MatrixInverseVPRotation[2]);
       wp.w = dot(tv, MatrixInverseVPRotation[3]);
       wp.xyz/= wp.w;
       wp.xyz+= CameraPosition;
return wp;
}

float4 wpd1(float2 cd)
{
float4 tv; 
       tv.xy = cd.xy*2.0-1.0;   
       tv.y =-tv.y;   
       tv.z = tex2Dlod(SamplerDepth, float4(cd.xy,0,0)).x;
       tv.w = 1.0;
float4 wp;
	   wp.x = dot(tv,MatrixInverseVPRotation[0]);
	   wp.y = dot(tv,MatrixInverseVPRotation[1]);
	   wp.z = dot(tv,MatrixInverseVPRotation[2]);
	   wp.w = dot(tv,MatrixInverseVPRotation[3]);
	   wp.xyz/= wp.w;	   
return wp;
}

float2 wpd2(float3 cd)
{
float4 tv = float4(cd.xyz, 1.0);
float4 wp = 0.0;
       wp.x = dot(tv,MatrixVPRotation[0]);
       wp.y = dot(tv,MatrixVPRotation[1]);
       wp.z = dot(tv,MatrixVPRotation[2]);
       wp.w = dot(tv,MatrixVPRotation[3]);
       wp.xyz/= wp.w;
       wp.y = -wp.y;
       wp.xy = wp.xy*0.5+0.5;	  
return wp.xy;
}

float3 fs0(float2 cd)
{
  float3 wpos = wpd1(cd.xy);
  float2 fscreen = float2(ScreenSize.y,ScreenSize.y*ScreenSize.z);
  float3 y = wpd1(cd.xy+float2(fscreen.x, 0.0)) - wpos;
  float3 f = wpd1(cd.xy+float2(0.0, fscreen.y)) - wpos;
  float3 n = cross(f,y);		 
  return normalize(n);
}

float4 reflection(float3 n, float2 cd)
{
   float3 wpos = wpd1(cd.xy);
   float3 v = float3(0.353, 0.353, 1.0);
          n = (normalize(n.xyz*v)*0.98);
   float3 n0 = reflect(wpos.xyz, n.xyz);	 
   float3 n1 = ((1000.0/0.01)*n0)/1000.0;
   float3 r0 = (wpos+n1);
   float2 r1 = wpd2(r0.xyz);
   float4 r2 = tex2Dlod(SamplerColor, float4(r1.xy, 0.0, 6.0)); 
	      r2.w = r1.y<0.0||r1.y>1.0 ? 0.0:1.0;
   return r2;
}



//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Lighting and Shadows
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
float4 PS_Lighting(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{
   float4 tv0;
   float4 tv1;
   float4 wpos;
   float4 npos; 
   float2 cd = IN.txcoord.xy;
   float4 r0 = tex2D(SamplerColor, cd);	
		  r0.a = min(1.0, r0.a);
   float4 r1 = tex2D(SamplerColor, cd);
   float4 r2 = r1;  
   float4 r3 = r1;  
   float4 r4 = r1;
	      r4.xyz+=0.000001; 
   float4 r5 = tex2D(SamplerOriginal, cd);	  		  
   float4 shadow = tex2D(SamplerShadow, cd);		  
   float3 normal = tex2D(SamplerNormal, IN.txcoord.xy)*2.0-1.0;
   float  d0 = tex2D(SamplerDepth, cd).x;
   float  d1 = linearlizeDepth(tex2D(SamplerDepth, cd).x)*1.0;

   float t0 = GameTime;
   float3 sv0 = SunDirection.xyz;
   float3 sv1 = normalize(float3(0.0, 0.0, 0.0));
   float s1 = smoothstep(0.0, 1.0, t0);	
   float s2 = smoothstep(1.0, 23.0, t0);
   float s3 = smoothstep(23.0, 24.0, t0);	
   float3 sunvec = lerp(sv1, sv0, s1);
          sunvec = lerp(sunvec, sv0, s2); 
          sunvec = lerp(sunvec, sv1, s3); 	  

   float x1 = smoothstep(0.0, 4.0, t0);
   float x2 = smoothstep(4.0, 5.0, t0);
   float x3 = smoothstep(5.0, 6.0, t0);
   float x4 = smoothstep(6.0, 7.0, t0);
   float xE = smoothstep(8.0, 11.0, t0);
   float x5 = smoothstep(16.0, 17.0, t0);
   float x6 = smoothstep(18.0, 19.0, t0);
   float x7 = smoothstep(19.0, 20.0, t0);
   float xG = smoothstep(20.0, 21.0, t0);  
   float xZ = smoothstep(21.0, 22.0, t0);
   float x8 = smoothstep(22.0, 23.0, t0);
   float x9 = smoothstep(23.0, 24.0, t0); 	   	  
 		 
   tv1.xy = IN.txcoord.xy*2.0-1.0;
   tv1.y = -tv1.y;
   tv1.z = d0;
   tv1.w = 1.0;
   wpos.x = dot(tv1, MatrixInverseVPRotation[0]);
   wpos.y = dot(tv1, MatrixInverseVPRotation[1]);
   wpos.z = dot(tv1, MatrixInverseVPRotation[2]);
   wpos.w = dot(tv1, MatrixInverseVPRotation[3]);
   wpos.xyz/= wpos.w;
	
   tv0.xy = normal.xy;
   tv0.x = -tv0.x;
   tv0.z = normal.z;
   npos.x = dot(tv0.xyz, MatrixInverseView[0])*1.0;
   npos.y = dot(tv0.xyz, MatrixInverseView[1])*1.0;
   npos.z = dot(tv0.xyz, MatrixInverseView[2])*1.0;
   float3 n2 = normalize(npos.xyz);
   float3 n3 = normalize(wpos.xyz);
  
   float4 wpos0 = float4(normalize(wpos.xyz), 1.0);	 
   float3 c1 = normalize(n3.xyz-sunvec);
          c1*= smoothstep(16.0, 0.0, normalize(-wpos0.z-sunvec));
		  c1*= saturate(1.0 - dot(r0.xyz, 0.0));

   float  d11 = dot(n2, sunvec);
          d11 = d11 * 1.0 + 0.0;		
   float  d12 = pow(d11 * 0.5 + 0.5, Lighting_Level);
		  
   float  d5 = dot(n2.xzy, normalize(float3(0.0, -1.0, 0.0))); 
          d5 = d5 * 1.0 + 0.0;		  
   float  d5s = dot(n2.xzy, normalize(float3(0.0, 1.0, 0.0))); 
          d5s = d5s * 1.0 + 0.0;		  
   float mix3 = pow(d5 * 0.5 + 0.5, 1.5);
   float mix3s = pow(d5s * 0.5 + 0.5, 1.5);  	  
   float4 sl = mix3*4.0; sl.a = min(1.0, sl.a);
   float4 sl2 = mix3s*4.0; sl2.a = min(1.0, sl2.a); 		  

   float3 timelt = lerp(0.0, 0.00, x1);
          timelt = lerp(timelt, float3(0.686, 0.392, 0.235)*float3(0.745, 0.588, 0.157)*7.0*0.3, x2);
          timelt = lerp(timelt, float3(0.686, 0.392, 0.235)*float3(0.745, 0.588, 0.157)*7.0, x3);
          timelt = lerp(timelt, 4.1*float3(1.0, 0.745, 0.51), x4);
          timelt = lerp(timelt, 4.1*float3(1.0, 0.98, 0.745)*0.90, xE);
          timelt = lerp(timelt, 4.1*float3(1.0, 0.98, 0.745)*0.90, x5);
          timelt = lerp(timelt, float3(0.686, 0.392, 0.235)*float3(0.745, 0.588, 0.157)*7.0, x6);		 
          timelt = lerp(timelt, 0.10, x7);
		  timelt = lerp(timelt, 0.10, xG);
		  timelt = lerp(timelt, 0.10, xZ);
          timelt = lerp(timelt, 0.10, x8);
          timelt*= 1.0;		  
   float3 timesh = lerp(0.0, 0.00, x1);
          timesh = lerp(timesh, 0.1, x2); 
          timesh = lerp(timesh, 0.1, x3);
          timesh = lerp(timesh, 0.2, x4);
          timesh = lerp(timesh, 0.2, xE);
          timesh = lerp(timesh, 0.2, x5);
          timesh = lerp(timesh, 0.0, x6);		 
          timesh = lerp(timesh, 0.1, x7);
		  timesh = lerp(timesh, 0.1, xG);
		  timesh = lerp(timesh, 0.1, xZ);
          timesh = lerp(timesh, 0.1, x8);  
          timesh = lerp(timesh, 0.0, x9); 		  	  
	
	float4 sh0 = shadow; 
	float4 sh1 = shadow;	
	float4 sh2 = shadow;	
    sh0*= saturate(0.0-(d11) * 2.0);
	sh0.xyz = lerp(1.0, sh0, timesh);
    sh1*= saturate(0.0-(d11) * 2.0);
	sh1.xyz = lerp(1.0, sh1, timesh*Lighting_Level);
	sh2*= saturate(-d11);
	float3 albedo = r0;
	       albedo = clamp(albedo, 0.001, 1.0);
	       albedo.xyz = albedo.xyz/(0.5+dot(albedo.xyz, 0.35));
		   
	float3 st0 = normalize(albedo.xyz);
	float3 cs0=albedo.xyz/st0.xyz;
	       cs0=pow(cs0, Shadow_Contrast);
		   st0.xyz = pow(st0.xyz, Shadow_Saturate);
	       albedo.xyz = cs0*st0.xyz;			   
   
	   float4 mask0 = tex2D(SamplerNormal, cd).w;	
	if (mask0.w<0.99) mask0 = 0.0;	
	   float4 mask1 = tex2D(SamplerNormal, cd).w;	
	if (mask1.w<0.99) mask1 = 50.0*1.9;
		              mask1*= 0.01;
	   float4 mask2 = tex2D(SamplerNormal, cd).w;	
	if (mask2.w<0.0) mask2 = 0.0;			
	   float4 mask3 = tex2D(SamplerNormal, cd).w;
	if (mask3.w==254/255.0) mask3 = 0.0;			
	   float4 mask4 = tex2D(SamplerNormal, cd).w;
	if (mask4.w==254/255.0) mask4 = 50.0*1.9;	
		                    mask4*= 0.01;			
	   float4 mask5 = tex2D(SamplerNormal, cd).w;
	if (mask5.w==253/255.0) mask5 = 5.0*100.0; 
		                    mask5*= 0.001;	
  float4 mask6 = r5; 
	     mask6.xyz+= 0.000001;
  float3 st1 = normalize(mask6.xyz);
  float3 cs1 = mask6.xyz/st1.xyz;
	     cs1 = pow(cs1, 1.0);
		 st1.xyz = pow(st1.xyz, 1.0);
	     mask6.xyz = cs1*st1.xyz;		 
  float4 mask7 = r5; 
	     mask7.xyz+= 0.000001;
  float3 st2 = normalize(mask7.xyz);
  float3 cs2 = mask7.xyz/st2.xyz;
	     cs2 = pow(cs2, 2.0);
		 st2.xyz = pow(st2.xyz, 0.1);
	     mask7.xyz = cs2*st2.xyz;
	float graymask0 = saturate(mask6);	
	float graymask1 = saturate(mask7);								

   float3 timesk = lerp(float3(0.843, 0.784, 0.667)*0.03, float3(0.843, 0.784, 0.667)*0.03, x1);
          timesk = lerp(timesk, float3(0.843, 0.784, 0.667)*0.6, x2);
          timesk = lerp(timesk, float3(0.843, 0.784, 0.667), x3);
          timesk = lerp(timesk, float3(0.843, 0.784, 0.667), x4);
          timesk = lerp(timesk, float3(0.843, 0.784, 0.667)*1.2, xE);
          timesk = lerp(timesk, float3(0.843, 0.784, 0.667)*1.2, x5);
          timesk = lerp(timesk, float3(0.843, 0.784, 0.667), x6); 
          timesk = lerp(timesk, float3(0.843, 0.784, 0.667)*0.1, x7);
		  timesk = lerp(timesk, float3(0.843, 0.784, 0.667)*0.05, xG);
		  timesk = lerp(timesk, float3(0.843, 0.784, 0.667)*0.05, xZ);
          timesk = lerp(timesk, float3(0.843, 0.784, 0.667)*0.03, x8);		  	  
   float3 timesk2 = lerp(float3(0.314, 0.431, 0.588)*1.0, float3(0.314, 0.431, 0.588)*1.0, x1);
          timesk2 = lerp(timesk2, float3(0.314, 0.431, 0.588)*0.6, x2);
          timesk2 = lerp(timesk2, float3(0.314, 0.431, 0.588), x3);
          timesk2 = lerp(timesk2, float3(0.314, 0.431, 0.588), x4);
          timesk2 = lerp(timesk2, float3(0.314, 0.431, 0.588), xE);
          timesk2 = lerp(timesk2, float3(0.314, 0.431, 0.588), x5);
          timesk2 = lerp(timesk2, float3(0.314, 0.431, 0.588), x6);	 
          timesk2 = lerp(timesk2, float3(0.314, 0.431, 0.588)*0.6, x7);
		  timesk2 = lerp(timesk2, float3(0.314, 0.431, 0.588)*0.6, xG);
		  timesk2 = lerp(timesk2, float3(0.314, 0.431, 0.588)*0.7, xZ);
          timesk2 = lerp(timesk2, float3(0.314, 0.431, 0.588)*0.7, x8);	  
          timesk2 = lerp(timesk2, float3(0.314, 0.431, 0.588)*1.0, x8);		  
 
float4 wx = WeatherAndTime;
float4 lc;
float4 ln;
float3 slc;
float3 sln;

	   float4 mask0z = tex2D(SamplerNormal, cd).w;	
	if (mask0z.w<0.50) mask0z = 0.0;	
	   float4 mask1z = tex2D(SamplerNormal, cd).w;	
	if (mask1z.w<0.99) mask1z = 50.0*40.0;
		              mask1z*= 0.001;
	float mask2z = lerp(1.0, 0.0, mask1z);				  
          mask2z = saturate(mask2z);					  
	float mask1x = saturate(mask1z*mask0z)*1.0;	

if (wx.x==0,1) slc = lerp(((timesk2*sl)+(sl2*timesk))*1.0, 1.0, mask1x);
if (wx.y==0,1) sln = lerp(((timesk2*sl)+(sl2*timesk))*1.0, 1.0, mask1x);
if (wx.x==4) slc = 1.0;
if (wx.x==7) slc = 1.0;
if (wx.x==8) slc = 1.0;
if (wx.x==9) slc = 1.0;
if (wx.x==12) slc = 1.0;
if (wx.x==15) slc = 1.0;
if (wx.x==16) slc = 1.0;
if (wx.y==4) sln = 1.0;
if (wx.y==7) sln = 1.0;
if (wx.y==8) sln = 1.0;
if (wx.y==9) sln = 1.0;
if (wx.y==12) sln = 1.0;
if (wx.y==15) sln = 1.0;
if (wx.y==16) sln = 1.0;
    float3 wmix = lerp(slc, sln, pow(wx.z,5.0));

	if(d1 < 3000)r0.xyz*= wmix*sh0*albedo*Shadow_Brightness;
	if(d1 < 3000)r0.xyz = lerp(r1, r0, mask0z); 
	if(d1 < 3000)r0.xyz = lerp(r1, r0, mask2z);	
	if(d1 < 3000)r1.xyz*= wmix*sh1*Shadow_Brightness;
    if(d1 < 3000)r0.xyz = lerp(r0.xyz, r1.xyz, mask1x);
	if(d1 < 3000)r0.xyz = lerp(r2.xyz, r0.xyz, mask2);	
	
	float3 st3 = normalize(r2.xyz);
	float3 cs3=r2.xyz/st3.xyz;
	       cs3=pow(cs3, Lighting_Contrast);
		   st3.xyz = pow(st3.xyz, Lighting_Saturate);
	       r2.xyz = cs3*st3.xyz;	
 
   float cz = 100.0*1.00;
   float f0 = pow(dot(c1.z, n2.z), 0.15);
         f0 = pow(f0, cz);
   float f1 = (f0*f0);
         f1/= 2.5;		 
		
   float3 timebl = lerp(0.0, 0.00, x1);
          timebl = lerp(timebl, float3(0.667, 0.51, 0.392)*float3(0.706, 0.51, 0.157)*7.0, x2);
          timebl = lerp(timebl, float3(0.667, 0.51, 0.392)*float3(0.706, 0.51, 0.157)*7.0*8.0, x3);
          timebl = lerp(timebl, 4.1*float3(1.0, 0.745, 0.51)*2.0, x4);
          timebl = lerp(timebl, 4.1*float3(1.0, 0.98, 0.745)*0.30, xE);
          timebl = lerp(timebl, 4.1*float3(1.0, 0.98, 0.745)*0.30, x5);
          timebl = lerp(timebl, float3(0.667, 0.51, 0.392)*float3(0.706, 0.51, 0.157)*7.0*8.0, x6);	 
          timebl = lerp(timebl, 0.0, x7);
		  timebl = lerp(timebl, 0.00, xG);
		  timebl = lerp(timebl, 0.00, xZ); 
          timebl = lerp(timebl, 0.00, x8);
          timebl*= 1.0;	

	if(d1 < 3000)r0.xyz+= lerp(r2*sh2*graymask0*timelt*(mask3*mask0)*Lighting_Brightness, graymask1*10.0*f1*timebl*PBR_Brightness*0.80, f1*graymask0*sh2*mask2z);
	 if(d1 < 3000)r0.xyz+= r3*sh2*(timelt*0.10)*(mask1+mask4)*Lighting_Brightness;
	 if(d1 < 3000)r0.xyz = lerp(r3, r0, mask0z);

	float3 wp = reflect(n3.xyz, n2.xyz);
    float4 pr = tex2D(SamplerEnv, wp.xzy);  
	float4 n0 = tex2D(SamplerNormal, cd);    
	float smix;		 
	      smix=saturate(npos.z)*saturate(-n0.z);
	      smix*=smix;	
	float nf1 = saturate(1.65+smix*0.3 -abs(n0.z));
	      nf1 = pow(nf1, 10.0);			 
   r0.xyz = lerp(r0, r0+pr*Peds_SideLighting, r2*nf1*mask5);
 
  float3 st4 = normalize(r4.xyz);
  float3 cs4=r4.xyz/st4.xyz;
	     cs4=pow(cs4, 2.1);
		 st4.xyz = pow(st4.xyz, 1.6);
	     r4.xyz = lerp(r4*0.6, cs4*st4.xyz, mask0*mask3);
   float4 t2;		 
          t2 = lerp(r4*1.5, r4*1.5, x1);
          t2 = lerp(t2, r0, x2);
          t2 = lerp(t2, r0, x3);
          t2 = lerp(t2, r0, x4);
          t2 = lerp(t2, r0, xE);
          t2 = lerp(t2, r0, x5);
          t2 = lerp(t2, r0, x6);		 
          t2 = lerp(t2, r0, x7);
          t2 = lerp(t2, r4*1.5, x8);
if (wx.x==0,1) lc = t2;
if (wx.y==0,1) ln = t2;
if (wx.x==4) lc = r4;
if (wx.x==7) lc = r4;
if (wx.x==8) lc = r4;
if (wx.x==9) lc = r4;
if (wx.x==12) lc = r4;
if (wx.x==15) lc = r4;
if (wx.x==16) lc = r4;
if (wx.y==4) ln = r4;
if (wx.y==7) ln = r4;
if (wx.y==8) ln = r4;
if (wx.y==9) ln = r4;
if (wx.y==12) ln = r4;
if (wx.y==15) ln = r4;
if (wx.y==16) ln = r4;
   float4 wmix0 = lerp(lc, ln, pow(wx.z, 5.0)); 
     
  //r0.xyz = mask2;
   //return r0;
   return wmix0;
   
}



//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Sky and Clouds
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
float4 	WorldPos(in float2 coord)
{
   float  d0 = tex2D(SamplerDepth, coord.xy).x;
   float4 tvec; 
          tvec.xy = coord.xy*2.0-1.0;   
          tvec.y = -tvec.y;   
          tvec.z = d0;
          tvec.w = 1.0;
   float4 wpos;
          wpos.x = dot(tvec, MatrixInverseVPRotation[0]);
          wpos.y = dot(tvec, MatrixInverseVPRotation[1]);
          wpos.z = dot(tvec, MatrixInverseVPRotation[2]);
          wpos.w = dot(tvec, MatrixInverseVPRotation[3]);
          wpos.xyz/= wpos.w;
   return wpos;
}
float3 CalculateSun(in float2 coord)
{	  
    float3 sv = SunDirection.xyz;
    float3 sv2 = normalize(float3(-0.0833, -0.946, 0.317));
	
   float t = GameTime;	
	
   float x1 = smoothstep(0.0, 4.0, t);
   float x2 = smoothstep(4.0, 5.0, t);
   float x3 = smoothstep(5.0, 6.0, t);
   float x4 = smoothstep(6.0, 7.0, t);
   float xE = smoothstep(8.0, 11.0, t);
   float x5 = smoothstep(16.0, 17.0, t);
   float x6 = smoothstep(18.0, 19.0, t);
   float x7 = smoothstep(19.0, 20.0, t);
   float xG = smoothstep(20.0, 21.0, t);  
   float xZ = smoothstep(21.0, 22.0, t);
   float x8 = smoothstep(22.0, 23.0, t);
   float x9 = smoothstep(23.0, 24.0, t);
   
   float3 t0 = lerp(0.0, 0.0, x1);
          t0 = lerp(t0, float3(1.0, 0.549, 0.0784)*2.0, x2);
          t0 = lerp(t0, float3(1.0, 0.549, 0.0784)*2.7, x3);
          t0 = lerp(t0, float3(1.0, 0.51, 0.235)*2.0, x4);
          t0 = lerp(t0, float3(1.0, 0.863, 0.549), xE);
          t0 = lerp(t0, float3(1.0, 0.863, 0.549), x5);
          t0 = lerp(t0, float3(1.0, 0.549, 0.0784)*2.0, x6);	 
          t0 = lerp(t0, float3(1.0, 0.549, 0.0784)*0.8, x7);
		  t0 = lerp(t0, float3(1.0, 0.549, 0.0784)*0.6, xG);
		  t0 = lerp(t0, float3(1.0, 0.549, 0.0784)*0.4, xZ);
          t0 = lerp(t0, float3(1.0, 0.549, 0.0784)*0.2, x8);
          t0 = lerp(t0, 0.0, x9);	

   float3 t3 = lerp(0.0, 0.0, x1);
          t3 = lerp(t3, float3(1.0, 0.447, 0.0)*2.0, x2);
          t3 = lerp(t3, float3(1.0, 0.447, 0.0)*2.7, x3);
          t3 = lerp(t3, float3(1.0, 0.784, 0.392)*2.0, x4);
          t3 = lerp(t3, float3(1.0, 1.0, 1.0)*2.0, xE);
          t3 = lerp(t3, float3(1.0, 1.0, 1.0)*2.0, x5);
          t3 = lerp(t3, float3(1.0, 0.447, 0.0)*2.0, x6);		 
          t3 = lerp(t3, float3(1.0, 0.447, 0.0)*0.8, x7);
		  t3 = lerp(t3, float3(1.0, 0.447, 0.0)*0.6, xG);
		  t3 = lerp(t3, float3(1.0, 0.447, 0.0)*0.4, xZ);
          t3 = lerp(t3, float3(1.0, 0.447, 0.0)*0.2, x8);
          t3 = lerp(t3, 0.0, x9); 	
   
   float4 wp = WorldPos(coord);
   float c0 = 475.0 * 25.0;
   float c1 = 18.0;
   float c2 = 1.35;
   
   float3 np0 = normalize(wp.xyz);
   float factor = (0.01/12.0) - dot(-sv, np0);
         factor = pow(factor, c0);
   float factor1 = 0.04 - dot(-sv, np0);
         factor1 = pow(factor1, c1);	
   float factor2 = 0.6 - dot(-sv, np0);
         factor2 = pow(factor2, c2);
		 
   float factor3 = 0.04 - dot(-sv2, np0);
         factor3 = pow(factor3, c1);	
   float factor4 = 0.6 - dot(-sv2, np0);
         factor4 = pow(factor4, c2);		 
		 
   float3 f0 = factor/12.0;
   float3 f1 = (factor1*factor1)/5.0;    
   float3 f2 = (factor2*factor2)/10.0;
   
   float3 f1x = (factor3*factor3)/5.0;    
   float3 f2x = (factor4*factor4)/10.0;   
   float3 fnight = (f1x*float3(0.0863, 0.137, 0.176))+(f2x*float3(0.0, 0.0, 0.0));     
		  fnight*= smoothstep(0.0, 0.3, -np0.y);   
   
   float y1 = smoothstep(0.0, 2.0, t);
   float yZ = smoothstep(2.0, 3.0, t);   
   float y2 = smoothstep(4.0, 23.0, t);
   float y3 = smoothstep(23.0, 24.0, t);   
   
  float3 t4 = lerp(0.0, 0.0, y1);
         t4 = lerp(t4, float3(0.0392, 0.0235, 0.0118), yZ);  
         t4 = lerp(t4, float3(0.0392, 0.0235, 0.0118), y2);
         t4 = lerp(t4, 0.0, y3);
		 
  float3 t5 = lerp(1.0, 0.5, y1);
         t5 = lerp(t5, 0.0, yZ);  
         t5 = lerp(t5, 0.0, y2);
         t5 = lerp(t5, 1.0, y3);	 
 
   float3 wp0 = wp + CameraPosition;  
   float3 np1 = normalize(wp0.xyz);
   float3 f3 = lerp(0.0, (f0*t4), smoothstep(0.0, 0.005, np1.z)) +(f1*t0)+(f2*t3)+(fnight*t5);
 
float3 SunCurrent;
float3 SunNext;
float4 wx = WeatherAndTime;

if (wx.x==0,1) SunCurrent = f3;
if (wx.y==0,1) SunNext = f3;

if (wx.x==4) SunCurrent = 0.0;
if (wx.x==7) SunCurrent = 0.0;
if (wx.x==8) SunCurrent = 0.0;
if (wx.x==9) SunCurrent = 0.0;
if (wx.x==12) SunCurrent = 0.0;
if (wx.x==15) SunCurrent = 0.0;
if (wx.x==16) SunCurrent = 0.0;

if (wx.y==4) SunNext = 0.0;
if (wx.y==7) SunNext = 0.0;
if (wx.y==8) SunNext = 0.0;
if (wx.y==9) SunNext = 0.0;
if (wx.y==12) SunNext = 0.0;
if (wx.y==15) SunNext = 0.0;
if (wx.y==16) SunNext = 0.0;	  

float3 wmix = lerp(SunCurrent, SunNext, wx.z);	   
 
   return wmix;
}

float CalculateGameTime0(in float t)
{	  
   float x1 = smoothstep(0.0, 4.0, t);
   float x2 = smoothstep(4.0, 5.0, t);
   float x3 = smoothstep(5.0, 6.0, t);
   float x4 = smoothstep(6.0, 7.0, t);
   float xE = smoothstep(8.0, 11.0, t);
   float x5 = smoothstep(16.0, 17.0, t);
   float x6 = smoothstep(18.0, 19.0, t);
   float x7 = smoothstep(19.0, 20.0, t);
   float xG = smoothstep(20.0, 21.0, t);  
   float xZ = smoothstep(21.0, 22.0, t);
   float x8 = smoothstep(22.0, 23.0, t);
   float x9 = smoothstep(23.0, 24.0, t);
   
   float3 t0 = lerp(0.0, 0.1, x1);
          t0 = lerp(t0, 0.2, x2);
          t0 = lerp(t0, 0.8, x3);
          t0 = lerp(t0, 0.9, x4);
          t0 = lerp(t0, 1.0, xE);
          t0 = lerp(t0, 1.0, x5);
          t0 = lerp(t0, 0.9, x6);	 
          t0 = lerp(t0, 0.5, x7);
		  t0 = lerp(t0, 0.4, xG);
		  t0 = lerp(t0, 0.3, xZ);
          t0 = lerp(t0, 0.2, x8);
          t0 = lerp(t0, 0.0, x9); 		  
   return t0;	  
}

float CalculateGameTime(in float t)
{	  
   float x1 = smoothstep(0.0, 4.0, t);
   float x2 = smoothstep(4.0, 5.0, t);
   float x3 = smoothstep(5.0, 6.0, t);
   float x4 = smoothstep(6.0, 7.0, t);
   float xE = smoothstep(8.0, 11.0, t);
   float x5 = smoothstep(16.0, 17.0, t);
   float x6 = smoothstep(18.0, 19.0, t);
   float x7 = smoothstep(19.0, 20.0, t);
   float xG = smoothstep(20.0, 21.0, t);  
   float xZ = smoothstep(21.0, 22.0, t);
   float x8 = smoothstep(22.0, 23.0, t);
   float x9 = smoothstep(23.0, 24.0, t);
   
   float3 t0 = lerp(0.0, 0.1, x1);
          t0 = lerp(t0, 0.7, x2);
          t0 = lerp(t0, 1.0, x3);
          t0 = lerp(t0, 1.0, x4);
          t0 = lerp(t0, 1.0, xE);
          t0 = lerp(t0, 1.0, x5);
          t0 = lerp(t0, 0.9, x6);		 
          t0 = lerp(t0, 0.8, x7);
		  t0 = lerp(t0, 0.6, xG);
		  t0 = lerp(t0, 0.4, xZ); 
          t0 = lerp(t0, 0.2, x8);
          t0 = lerp(t0, 0.0, x9); 		  
   return t0;	  
}

float CalculateGameTime2(in float t)
{	  
   float x1 = smoothstep(0.0, 4.0, t);
   float x2 = smoothstep(4.0, 5.0, t);
   float x3 = smoothstep(5.0, 6.0, t);
   float x4 = smoothstep(6.0, 7.0, t);
   float xE = smoothstep(8.0, 11.0, t);
   float x5 = smoothstep(16.0, 17.0, t);
   float x6 = smoothstep(18.0, 19.0, t);
   float x7 = smoothstep(19.0, 20.0, t);
   float xG = smoothstep(20.0, 21.0, t);  
   float xZ = smoothstep(21.0, 22.0, t);
   float x8 = smoothstep(22.0, 23.0, t);
   float x9 = smoothstep(23.0, 24.0, t);
   
   float3 t0 = lerp(0.0, 0.0, x1);
          t0 = lerp(t0, 0.7, x2);
          t0 = lerp(t0, 0.9, x3);
          t0 = lerp(t0, 1.0, x4);
          t0 = lerp(t0, 1.0, xE);
          t0 = lerp(t0, 0.5, x5);
          t0 = lerp(t0, 0.5, x6);		 
          t0 = lerp(t0, 1.0, x7);
		  t0 = lerp(t0, 1.0, xG);
		  t0 = lerp(t0, 1.0, xZ);
          t0 = lerp(t0, 0.0, x8);  
          t0 = lerp(t0, 0.0, x9);	  
   return t0;	  
}

float3 ColorHorizon(float3 r0, float f)
{	
   float t = GameTime;
   float tf = CalculateGameTime2(t); 
   float df = pow(saturate(SunDirection.z * tf), 1.0);
	float3 sg = lerp(1.0, float3(1.000, 0.804, 0.863), pow(r0.y, 2.2));
	float3 sg2 = lerp(1.0, float3(1.000, 1.000, 0.584), pow(r0.y, 1.15));
	float3 sd = lerp(1.0, float3(1.000, 1.000, 1.000), pow(r0.y, 2.15));
	float3 sd2 = lerp(1.0, float3(1.000, 1.000, 1.000), pow(r0.y, 1.15));
	float3 a = lerp(sg, 1.0, f);	
	float3 a2 = lerp(sg2, 1.0, f);
	float3 ad = lerp(sd, 1.0, f);	
	float3 ad2 = lerp(sd2, 1.0, f);
	r0 *= lerp(a, 1.0, df);	
	r0 *= lerp(a2, 1.0, df);
	float3 d1 = lerp(1.0, ad, df);
	float3 d2 = lerp(1.0, ad2, df);
    r0 *= d1*d2;				
   return r0;
}

float3 ColorTop()
{	
float3   cc = float3(1.0, 1.0, 1.0);
float3   cn = float3(1.0, 1.0, 1.0);

float x1 = smoothstep(1.0, 2.0,  WeatherAndTime);
   
float4 wx = WeatherAndTime;

if (wx.x==0,1) cc = float3(0.251, 0.51, 1.0);
if (wx.y==0,1) cn = float3(0.251, 0.51, 1.0);

float3 skyW = float3(0.259, 0.259, 0.259);

if (wx.x==4) cc = skyW;
if (wx.x==7) cc = skyW;
if (wx.x==8) cc = skyW;
if (wx.x==9) cc = skyW;
if (wx.x==12) cc = skyW;
if (wx.x==15) cc = skyW;
if (wx.x==16) cc = skyW;

if (wx.y==4) cn = skyW;
if (wx.y==7) cn = skyW;
if (wx.y==8) cn = skyW;
if (wx.y==9) cn = skyW;
if (wx.y==12) cn = skyW;
if (wx.y==15) cn = skyW;
if (wx.y==16) cn = skyW;

float3 wmix0 = lerp(cc, cn, wx.z);

return wmix0;
}

float4 	GWP(in float2 c, in float d)
{
   float4 t; 
   float4 w;
   t.xy=c.xy*2.0-1.0;
   t.y=-t.y;
   t.z=d;
   t.w=1.0;
   w.x=dot(t, MatrixInverseVPRotation[0]);
   w.y=dot(t, MatrixInverseVPRotation[1]);
   w.z=dot(t, MatrixInverseVPRotation[2]);
   w.w=dot(t, MatrixInverseVPRotation[3]);
   w.xyz/=w.w;
   //w.xyz+=CameraPosition;
   return w;
}

float Coverage(in float v, in float d, in float c)
{
	c = clamp(c - (1.0 - v), 0.0, 1.0 - d)/(1.0 - d);
	c = max(0.0, c * 1.1 - 0.1);
	c = c = c * c * (3.0 - 2.0 * c);
	return c;
}

#define nsA		0.0005/noiseSizeA
#define nsB		0.0005/noiseSizeB
#define nsC		0.1/noiseSizeC
#define nsD		0.1/noiseSizeD
#define nsE		0.1/noiseSizeE

float3 PosOnPlane(const in float3 origin, const in float3 direction, const in float h, inout float dis)
{
    dis = (h - origin.z) / direction.z;
    
    return float3((origin + direction * dis).xy, h);
}

float3 PosOnSphere(const in float3 o, const in float3 d, const in float h, const in float r, inout float distance)
{
    float d1 = -h * d.z,
    d2 = sqrt(r * r - (h * h - d1 * d1));
    distance = d1 + d2;
    return o + d * distance;
}

float clampMap(float x, float a, float b, float c, float d)
{
    return clamp((x - b) / (a - b), 0.0, 1.0) * (c - d) + d;

}

float4 cloud_shape(float z, float4 profile, float3 dens_thres)
{
    float soft = clampMap(z, profile.y, profile.x, dens_thres.z, dens_thres.y);
    
    return
        float4(
        smoothstep(profile.z, lerp(profile.y, profile.z, profile.w), z) * smoothstep(profile.x, lerp(profile.y, profile.x, profile.w), z),
        dens_thres.x + soft,
        dens_thres.x - soft,
        soft);
}

//============IQ's noise===================
//This noise generator is developed by Inigo Quilez.
//License: Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
float hash(float n)
{
    return frac(sin(n) * 9487.5987);
}

float noise_p(float3 p)
{
    float3 fr = floor(p),
       ft = frac(p);
    
    
    float n = 3.0 * fr.x + 17.0 * fr.y + fr.z,
        nr = n + 3.0,
        nd = n + 17.0,
        no = nr + 17.0,
        
        v = lerp(hash(n), hash(n + 1.0), ft.z),
        vr = lerp(hash(nr), hash(nr + 1.0), ft.z),
        vd = lerp(hash(nd), hash(nd + 1.0), ft.z),
        vo = lerp(hash(no), hash(no + 1.0), ft.z);
    
    return ((v * (1.0 - ft.x) + vr * (ft.x)) * (1.0 - ft.y) +
        (vd * (1.0 - ft.x) + vo * ft.x) * ft.y);
}
//=========================================

float noise_d(const in float2 xy)
{
    return tex2Dlod(SamplerClouds, float4(xy, 0.0, 0.0)).r;
}

//large chunk for the distribution of clouds.
float chunk(in float3 pos, const in float3 offsetA, const in float3 offsetB, in float cs)
{
    pos += cloud_shift * pos.z;
    float2
		pA = pos.xy + offsetA.xy,
		pB = pos.xy + offsetB.xy;

    return clamp((noise_densA * noise_d(pA * nsA) + noise_densB * noise_d(pB * nsB)) * cs, 0.0, 1.0);
}

//Detial noise for clouds
float detial(const in float3 pos, const in float3 offsetC, const in float3 offsetD)
{
    float3
		pC = pos + offsetC,
		pD = pos + offsetD;
    
    float nc = noise_p(pC * nsC);
    
    //curvy distortion
    float a = nc * curvy_ang;
    float3 d = float3(cos(a), sin(a), 0.0) * curvy_strength;
    
    float dens = noise_densC * nc + noise_densD * (1.0 - noise_p((pD + d) * nsD)) + noise_densE * noise_p((pD + d / 2) * nsE);
	
    return dens;
}

float detial2(const in float3 pos, const in float3 offsetC)
{
    float3
		pC = pos + offsetC;
	
    return noise_densC * noise_p(pC * nsC) * 2.0;
}

//one step marching for lighting
float brightness(float3 p, float4 profile, float3 dens_thres, const in float3 offsetA, const in float3 offsetB, const in float3 offsetC)
{
    float3
        p1 = p + SunDirection.xyz * ShadowStepLength,
        p2 = p + SunDirection.xyz * DetailShadowStepLength;
    
    float4
        cs = cloud_shape(p1.z, profile, dens_thres);
        
    float
        dens1 = chunk(p1, offsetA, offsetB, cs.x),
		dens2 = detial2(p2, offsetC) * ShadowDetail,
    
    d = (dens1 + dens2) * shadowContrast + shadowExpand - (cs.z - shadowSoftness);
    d /= (cs.w + shadowSoftness) * 2.0;
        
    return 1.0 - clamp(d, 0.0, 1.0) * shadowStrength;
}

float4 Cloud_mid(const in float3 cam_dir, in float3 p, in float dist, const in float depth, float time, float moonLight, float tf, float sunny)
{
    float4 d = float4(0.0, 0.0, 0.0, min_sl); //distance, distance factor, gap, the last march step
    
    p = PosOnPlane(p, cam_dir, clamp(p.z, vb_b, vb_t), d.x);
    d.y = d.x;
    
    if (d.x >= 0.0 && d.x < fade_e)
    {
        float
            fe = lerp(fogEffect_c, fogEffect, sunny) / 100.0;
        
        float3
			flowA = -time * nA_move,
			flowB = -time * nB_move,
			flowC = -time * nC_move,
			flowD = -time * nD_move,
        
            dens_thres = float3(
                1.0 / lerp(grow_c, grow, sunny),
                soft_top,
                lerp(soft_bot_c, soft_bot, sunny)),
        
            fx = float3(0.5, 0.0, 1.0); //brightness, density, transparency
            
        float4 profile = float4(body_top, body_mid, body_bot, body_thickness);
        
        if (depth == 1)
            dist = fade_e;
        
        for (int i = 0.0; fx.z > 0.0 && p.z <= vb_t && p.z >= vb_b && i < maxStep && d.x - d.w < dist; i += 1.0)
        {
            float3 cs = cloud_shape(p.z, profile, dens_thres).xyz;
            
            float
                dens = (chunk(p, flowA, flowB, cs.x) + temp2) * (detial(p, flowC, flowD) + temp1) / (1 + temp2) / (1 + temp1),
                marchStep = clampMap(dens, cs.z, dCut, min_sl, max_sl) * clampMap(d.x, 0.0, 10000.0, 1.0, step_mul), 
			    coverage = smoothstep(cs.z, cs.y, dens) * marchStep / CloudSoftness,
                ds = fx.z + fe;
            
            //fade into near objects
            if (d.x >= dist)
            {
                coverage *= d.z / d.w;
                fx.y += fe * d.z / d.w;
            }
            else
                fx.y += fe;
            
            ds *= coverage / EffectFactor;
            fx.x = lerp(fx.x, brightness(p, profile, dens_thres, flowA, flowB, flowC), ds);
            d.y = d.y * (1.0 - ds) + ds * d.x;
            
            fx.y += coverage;
            fx.z = (exp(-fx.y / BeerFactor) - tCut) / (1.0 - tCut);
            
            d.z = dist - d.x;
            
            //marching
            p += cam_dir * marchStep;
            d.x += marchStep;
            d.w = marchStep;
        }
        
        if (fx.y > 0.0)
        {
            float
			    noon_index = smoothstep(-0.09, 0.3, tf),
			    sunset_index = smoothstep(0.3, 0.1, tf) * smoothstep(-0.18, -0.08, tf),
			    suncross = dot(SunDirection.xyz, cam_dir);
            
            float3
			    c_dark = lerp(lerp(lerp(CBC_CN, CBC_CD, noon_index), lerp(CBC_N, CBC_D*10.0, noon_index), sunny), CBC_S*5.0, sunset_index),
			    c_bright = lerp(lerp(lerp(CTC_CN, CTC_CD, noon_index), lerp(CTC_N, CTC_D, noon_index), sunny), CTC_S, sunset_index) * cloudBrightnessMultiply;
            
            c_bright += normalize(c_bright) * scatteringStrength * exp((suncross - 1.0) / scattering) * (smoothstep(-0.13, -0.08, tf));
            
            float3 C = lerp(c_dark, c_bright, smoothstep(0.0, 1.0, (fx.x - 0.5) * CloudContrast + 0.5));
            
            float rsf = smoothstep(Atomesphere_Distance - Atomesphere_Smoothness, Atomesphere_Distance + Atomesphere_Smoothness, d.y);
            C += Atomesphere / 100.0 * rsf;

		    //fading
            fx.y = 1.0 - exp(-fx.y / alpha_curve);
            fx.y *= smoothstep(fade_e, fade_s, d.y);
            return float4(C, fx.y);
        }
    }
    return float4(0.0, 0.0, 0.0, 0.0);
}


//high cloud
float Density_h(const in float3 pos, const in float3 flowA, const in float3 flowB)
{
    float3
		pA = pos + flowA,
		pB = pos + flowB;
    
    float a = noise_p(pB / curvy_scale_h) * curvy_ang_h;
    
    float3 d = float3(cos(a), sin(a), 0.0) * curvy_strength_h;

    float
		densA = H_noise_densA * noise_p((pA + d) / H_scaleA),
		densB = H_noise_densB * noise_p((pB + d) / H_scaleB) * densA;

    return densA + densB;
}

float4 Cloud_high(const in float3 cam_dir, const in float3 cam_pos, const in float dist, const in float depth, float time, float height, float moonLight, float tf, float sunny)
{
    float distance = 0.0;
    float3 position = PosOnSphere(cam_pos, cam_dir, cam_pos.z + 1000000.0, height + 1000000.0, distance);

    if (distance > 0.0 && (depth == 1.0 || distance < dist) && cam_dir.z > -0.1)
    {
        float
			alpha = 0.0,
			noon_index = smoothstep(0.0, 0.3, tf),
			sunset_index = smoothstep(0.3, 0.1, tf) * smoothstep(-0.2, -0.1, tf) * sunny,
			suncross = dot(SunDirection.xyz, cam_dir),
			nearMoon = moonlight_strength * smoothstep(0.9, 1.2, dot(float3(-0.12, -0.9, 0.45), cam_dir)) * moonLight;
        
        float3
			c_dark = lerp(lerp(lerp(CBC_CN, CBC_CD, noon_index), lerp(CBC_N, CBC_D, noon_index), sunny), CBC_S, sunset_index),
			c_bright = lerp(lerp(lerp(CTC_CN, CTC_CD, noon_index), lerp(CTC_N, CTC_D, noon_index), sunny), CTC_S, sunset_index);
        
        c_bright += normalize(c_bright) * scatteringStrength_h * exp((suncross - 1.0) / scattering / 1.5);

        float3 C = float4(0.0, 0.0, 0.0, 0.0),
			flowA = -time * H_nA_move,
			flowB = -time * H_nB_move;

        alpha = Density_h(position / 1500.0, flowA, flowB);

		//density threshold
        alpha *= smoothstep(grow_h - softness_h, grow_h + softness_h, alpha);
        
        alpha *= smoothstep(-0.1, 0.1, cam_dir.z);
        
        C = lerp(c_bright, c_dark, alpha);

        return float4(C, alpha);
    }
    return float4(0.0, 0.0, 0.0, 0.0);
}

//maybe fixed?
float SunnyFactor()
{
    float4 c, w = WeatherAndTime;
    
    if (w.x <= 3 || w.x == 6 || w.x == 11 || w.x == 13 || w.x == 14 || w.x == 15 || w.x == 17)
        c.x = 1.0;
    else
        c.x = 0.0;
    
    if (w.x <= 3 || w.x == 6 || w.x == 11 || w.x == 13 || w.x == 14 || w.x == 15 || w.x == 17)
        c.y = 1.0;
    else
        c.y = 0.0;
    
    return lerp(c.x, c.y, w.z);
}

float Stars(in float2 coord)
{	
  //float t = (Timer.x * 10.0) * 0.25;
    float2 uv = coord.xy;
	float2 p = (uv) / (starscover*2.0);
	float fstars = tex2Dlod(SamplerStars, float4(p, 0.0, 0.0));
	return fstars;
}

float Moon(in float2 coord)
{	
    float2 uv = coord.xy + 0.1 * 1.5;
	float2 p = (uv + 0.5) / (-3.25*1.25);
	float fmoon = tex2Dlod(SamplerMoon, float4(p, 0.0, 0.0));
	return fmoon*1.5;
}



float4 PS_Sky(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{
   float t = GameTime;
   float tf = CalculateGameTime(t);   
   float tf2 = CalculateGameTime0(t);    
   
   float2 coord = IN.txcoord.xy;
   float4 r0 = tex2D(SamplerColor, coord.xy);
   float  d0 = tex2D(SamplerDepth, coord.xy).x;
   float4 wpos = WorldPos(coord);
   float3 colorS = ColorTop();  
//Sky------------------------------------------------------------------------------	
   float4 w0 = wpos+CameraPosition;
   float4 cam = CameraPosition; 
   float p2 = length(wpos.xyz);  
   float3 dp = -1.0/exp(length(0.0-p2)/2048.0);  
   float fog = pow(tex2D(SamplerDepth, coord).x, 117000.0);
         fog = lerp(1.0, fog, dp); 		 
   float4 f0 = pow(fog.x, -1000.0);
          f0.a = min(1.0, f0.a); 	  
   float4 np0 = float4(normalize(wpos.xyz), 1.0);
	      np0.xyz = normalize(w0.xyz-float3(cam.xy, 0.0));	
		  
   float3 vec = normalize(float3(0.0, 0.0, 1.0));	  
   float3 hv = normalize(-vec+np0);
   float sgf = dot(hv, np0);    
   
 	if (dot(hv, np0) > 0.75*0.95)
		  sgf = (1.5*0.95) - sgf;
	float curve = 8.0*0.85;	
	      sgf = pow(sgf, curve);  

	float3 d1 = float3(1.0, 1.0, 1.0);	
 		   d1 = CL(d1) * colorS;
		   d1 *= (sgf * (20.0*1.4)) + 0.85;
		   
   float3 sun = CalculateSun(coord);		   
   float3 d2 = ColorHorizon(d1, sgf);
   
	d1 = d2;
	d1*= lerp(0.0, 1.0, tf);
	d1.xyz+=0.000001;
	float3 n0 = normalize(d1.xyz);
	float3 ct0=d1.xyz/n0.xyz;
	ct0=pow(ct0, Sky_Contrast);
    n0.xyz = pow(n0.xyz, Sky_Saturate);   
    d1.xyz = ct0*n0.xyz;  
    d1.xyz*= Sky_Brightness;
	
    float np4 = (np0.z * (2.0)) + 0.40;
	      np4 *= lerp(0.0, 1.0, tf2);  		  
    float3 a = d1+sun;
    float3 a2 = np4;
    float3   sc = 1.0;
    float3   sn = 1.0;   
    float4 wx = WeatherAndTime;	
    if (wx.x==0,1) sc = a;
    if (wx.y==0,1) sn = a;   
    if (wx.x==4) sc = a2;
    if (wx.x==7) sc = a2;
    if (wx.x==8) sc = a2;
    if (wx.x==9) sc = a2;
    if (wx.x==12) sc = a2;
    if (wx.x==15) sc = a2;
    if (wx.x==16) sc = a2;
    if (wx.y==4) sn = a2;
    if (wx.y==7) sn = a2;
    if (wx.y==8) sn = a2;
    if (wx.y==9) sn = a2;
    if (wx.y==12) sn = a2;
    if (wx.y==15) sn = a2;
    if (wx.y==16) sn = a2;
    float3 wmix0 = lerp(sc, sn, wx.z);			
    r0.xyz = lerp(r0, wmix0, f0);	
   float4  worldpos = GWP(coord, d0);
   float L = worldpos.xyz;
//NightSky-------------------------------------------------------------------------
   float sm1 = smoothstep(0.0, 4.0, t),	
         sm2 = smoothstep(4.0, 5.0, t),	 
         sm3 = smoothstep(5.0, 6.0, t),			 
         sm4 = smoothstep(6.0, 23.0, t),
         sm5 = smoothstep(23.0, 24.0, t);
   float3 ti = lerp(1.0, 1.0, sm1);
          ti = lerp(ti, 0.3, sm2);   
          ti = lerp(ti, 0.0, sm3); 		  
          ti = lerp(ti, 0.0, sm4);
          ti = lerp(ti, 1.0, sm5);    
   float3 p0 = (MatrixInverseVP[3].xyz/MatrixInverseVP[3].w);		  
   float3 ns0 = normalize(worldpos.xzy);
   float3 ns1 = normalize(worldpos.xyz);
          ns1.z = abs(ns1.z)*1.40;	
   float3x3 vec0 = float3x3(1, 0, 0, 0, cos(2.91), sin(2.91), 0, -sin(2.91), cos(2.91));   
		   ns0 = mul(ns0, vec0);		   
	float3 np1 = normalize(ns0);
    float3 np2 = normalize(-p0+ns1);
    float4 np3 = float4(normalize(worldpos.xyz), 1.0); 		
    if( 10000 < L || d0 == 1)
      {
          float4 s0 = Stars(np2 * 0.5 + 0.5)*3.2*Stars_Brightness;
		         s0*= step(0.3, s0);
				 s0*= smoothstep(0.0, 0.3, np3.z);
			     s0*= saturate(1.0 - dot(r0.xyz, 4.0));
				 s0.a = min(1.0, ti*s0.a);
                 r0.xyz = lerp(r0, s0.xyz, s0.a);
      }
    if( 6000 < L || d0 == 1)
      {
          float4 m0 = Moon(np1*50.0)*1.00;
		  		 m0*= smoothstep(0.0, 0.3, -np3.y);
				 m0.a = min(1.0, ti*m0.a);
                 r0.xyz = lerp(r0, 1.65*m0.xyz, m0.a);
      }
//Clouds---------------------------------------------------------------------------
    float
        tid = SunDirection.z,
        moon = 5.0,
        sunny = SunnyFactor(),
        dist = distance(float3(0, 0, 0), worldpos.xyz);
 
    float3 direction = normalize(worldpos.xyz);
    
    //clouds
    float4 highCloud = Cloud_high(direction, CameraPosition.xyz, dist, d0, Timer.x * speed, 2000.0, moon, tid, sunny);
    r0.xyz = lerp(r0, highCloud.rgb, highCloud.a);
    
    float4 midCloud = Cloud_mid(direction, CameraPosition.xyz, dist, d0, Timer.x * 500.0 * speed, moon, tid, sunny);
    r0.xyz = lerp(r0, midCloud.rgb, midCloud.a);
    
    return r0;
}



//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Rain
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
float Rain0(in float2 cd)
{	
    float2 uv = cd.yx;
	float2 p = uv/5.0;
	float r = tex2Dlod(SamplerRain, float4(p*float2(5.0, 0.5), 0.0, 0.0)).y;
	return r;
}

float Rain1(in float2 cd)
{	
    float2 uv = cd.yx;
	float2 p = uv/1.2;
	float r = tex2Dlod(SamplerRain, float4(p*float2(3.5, 0.3), 0.0, 0.0)).y;	
	return r;
}

float Rain2(in float2 cd)
{	
    float2 uv = cd.yx;
	float2 p = uv/0.5;
	float r = tex2Dlod(SamplerRain, float4(p*float2(3.0, 0.5), 0.0, 0.0)).y;
	float r2 = tex2Dlod(SamplerRain, float4(p*float2(2.0, 0.5), 0.0, 0.0)).y;
	float j = (r+r2);
	return j;
}

float3 ComputeRipple(float2 uv, float CurrentTime, float Weight)
{
//https://seblagarde.wordpress.com/2013/01/03/water-drop-2b-dynamic-rain-and-its-effects/	
    float4 rp = tex2Dlod(SamplerWrp, float4(uv, 0.0, 0.0))*1.4;
           rp.yz = rp.yz * 2.0-1.0;	
    float a0 = tex2Dlod(SamplerAlpha, float4(uv, 0.0, 0.0))*1.4;
    float a1 = tex2Dlod(SamplerAlpha2, float4(uv, 0.0, 0.0))*1.4;	

	float4 rp3 = lerp(0.0, rp, a0);
		   
float4  wx = WeatherAndTime;	   
float  wc;
float  wn;
if (wx.x==0,1) wc = 0.0;
if (wx.y==0,1) wn = 0.0;
if (wx.x==8)   wc = 2.0;
if (wx.x==16)  wc = 2.0;
if (wx.y==8)   wn = 2.0;
if (wx.y==16)  wn = 2.0;	  
      float rp0 = lerp(wc, wn, pow(wx.z, 0.5));
	   float DropFrac = frac(rp3.w+CurrentTime);
	   float TimeFrac = DropFrac - 1.0 + rp3.x;
	   float DropFactor = saturate(0.20 + Weight * 0.80 - DropFrac);
	   float PI = 3.14159265359;
	   float FF = DropFactor*rp3.x * sin(clamp(TimeFrac * 12.0, 0.0, 5.0) * PI);
	   
	   float3 cpl = lerp(0.0, float3(rp3.yz*FF*rp0, 1.0), a1);
	   return cpl;
}

float3 CR(float3 n, float3 uv)
{
float Tr = Timer.x * 14000.0;
float4 TimeMul = float4(0.6, 0.7, 0.8, 1.0); 
float4 TimeAdd = float4(0.22, 0.44, 0.66, 0.9);
float4 Times = (Tr * TimeMul + TimeAdd) * 2.4;
       Times = frac(Times);
float4 Weights = 1.0 - float4(0, 0.25, 0.5, 0.75);
       Weights = saturate(Weights * 4.0);
float3 Ripple1 = ComputeRipple(uv * 0.18 + float2(0.25, 0.0),  Times.x, Weights.x);
float3 Ripple2 = ComputeRipple(uv * 0.25 + float2(-0.55, 0.3), Times.y, Weights.y);
float3 Ripple3 = ComputeRipple(uv * 0.28 + float2(0.6, 0.85),  Times.z, Weights.z);
float3 Ripple4 = ComputeRipple(uv * 0.30 + float2(0.5, -0.75),  Times.w, Weights.w);
float3 Ripple5 = ComputeRipple(uv * 0.32 + float2(0.3, -0.42),  Times.w, Weights.w);
float3 Ripple6 = ComputeRipple(uv * 0.20 + float2(0.35, 0.1),  Times.x, Weights.x);
	n=normalize(float3(n.xy+Ripple1.xy,n.z));
	n=normalize(float3(n.xy+Ripple2.xy,n.z));
	n=normalize(float3(n.xy+Ripple3.xy,n.z));
	n=normalize(float3(n.xy+Ripple4.xy,n.z));	
	n=normalize(float3(n.xy+Ripple5.xy,n.z));
	n=normalize(float3(n.xy+Ripple6.xy,n.z));	
	return n;	
}

float3 ComputeRelief(float2 uv, float CurrentTime, float3 Weight)
{
    float tx = (Timer.x * 1000.0) * 1.05;
    float4 rf = tex2Dlod(SamplerWrl,  float4(uv.xy + float2(0.5, 0.5*tx), 0.0, 0.0));
		   rf.yz = rf.yz * 2.0-1.0;		   
       float RelfSt = step(0.2, rf.a);		   
	   float DropFrac = frac(rf.w+CurrentTime);
	   float3 DropFactor = saturate(0.2 + Weight * 0.8 - DropFrac);	
	   float3 FF = DropFactor * rf.x*RelfSt;
    return float3(rf.yz*FF, 1.0);
}

float3 PR(float3 n, float3 uv)
{
float4 t = 1.99;
float3 w = float3(1.2, 1.2, 1.2);
float tx = (Timer.x * 1000.0) * 1.05;	   
float3 Relief1 = ComputeRelief(uv * 0.05 + float2(0.55, 0.89),  t.x, w);
float3 Relief2 = ComputeRelief(uv * 0.25 + float2(0.22, 0.33)*tx,  t.x, w);	
	n=normalize(float3(n.xy+Relief1.xy,n.z)); 
	n=normalize(float3(n.xy+Relief2.xy,n.z));
    return n;
}

float Lightning(in float2 coord)
{	
    float2 uv = coord.xy + 0.1 * 1.5;
	float2 p = (uv + 0.5) / (-3.25*2.0);
	float fl0 = tex2Dlod(SamplerLightning, float4(p, 0.0, 0.0));
	float fl1 = tex2Dlod(SamplerLightning2, float4(p, 0.0, 0.0));	
	float ti = max(sin(Timer.x*100000.0*2.0),0);			
	float mix = lerp(fl0, fl1, ti);	
	return mix*1.5;
}

float Lightning2(in float2 coord, float v1, float v2)
{	
    float2 uv = coord.xy + 0.1 * 1.5;
	float2 p = (float2(v1, v2)+ uv + 0.5) / (-3.25*2.0);
	float fl0 = tex2Dlod(SamplerLightning, float4(p, 0.0, 0.0));
	float fl1 = tex2Dlod(SamplerLightning2, float4(p, 0.0, 0.0));	
	float ti = max(sin(Timer.x*100000.0*2.0),0);			
	float mix = lerp(fl0, fl1, ti);	
	return mix*1.5;
}

float4 PS_Rain(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{
   float4 r0 = tex2D(SamplerColor, IN.txcoord.xy);	
   float4 r1 = r0;	   
   float2 cd = IN.txcoord.xy;
   float d0 = tex2D(SamplerDepth, IN.txcoord.xy).x;
   float d1 = linearlizeDepth(tex2D(SamplerDepth, IN.txcoord.xy).x)*1.0;	   
   float4 tv0;
   float4 wpos;
   tv0.xy = IN.txcoord.xy*2.0-1.0;
   tv0.y =-tv0.y;
   tv0.z = d0;
   tv0.w = 1.0;
   wpos.x = dot(tv0, MatrixInverseVPRotation[0]);
   wpos.y = dot(tv0, MatrixInverseVPRotation[1]);
   wpos.z = dot(tv0, MatrixInverseVPRotation[2]);
   wpos.w = dot(tv0, MatrixInverseVPRotation[3]);
   wpos.xyz/= wpos.w;
   float4 npos;
   float4 n0 = tex2D(SamplerNormal, IN.txcoord.xy)*2.0-1.0;
   float4 tv1;
   tv1.xy = n0.xy;
   tv1.x = -tv1.x;
   tv1.z = n0.z;
   npos.x = dot(tv1.xyz, MatrixInverseView[0]);
   npos.y = dot(tv1.xyz, MatrixInverseView[1]);
   npos.z = dot(tv1.xyz, MatrixInverseView[2]);
   float3 n2 = normalize(npos.xyz);	  
          n2 = CR(n2.xyz, wpos+CameraPosition);  
          n2 = PR(n2.xyz, wpos+CameraPosition);  		  
    float3 vp = float3(0.0, 0.0, 1.03);
    float4 wc = wpd0(cd, d0);
	float rx = tex2Dlod(SamplerPuddle, float4(wc.xy*0.012, 0.0, 0.0));	
float t = GameTime;		
   float x1 = smoothstep(0.0, 4.0, t);
   float x2 = smoothstep(4.0, 5.0, t);
   float x3 = smoothstep(5.0, 6.0, t);
   float x4 = smoothstep(6.0, 7.0, t);
   float xE = smoothstep(8.0, 11.0, t);
   float x5 = smoothstep(16.0, 17.0, t);
   float x6 = smoothstep(18.0, 19.0, t);
   float x7 = smoothstep(19.0, 20.0, t);
   float xG = smoothstep(20.0, 21.0, t);  
   float xZ = smoothstep(21.0, 22.0, t);
   float x8 = smoothstep(22.0, 23.0, t);
   float x9 = smoothstep(23.0, 24.0, t);     	
float rc;
float rn;
float4 Wx = WeatherAndTime;	
if (Wx.x==0,1) rc = 2.0;
if (Wx.y==0,1) rn = 2.0;
if (Wx.x==8)   rc = 0.2;
if (Wx.y==8)   rn = 0.2;
if (Wx.x==16)  rc = 0.2;
if (Wx.y==16)  rn = 0.2;	  
float wmix1 = lerp(rc, rn, pow(Wx.z, 8.5));
	float coverage = lerp(wmix1, Puddles_alltime_coverage, Puddles_alltime);
		  coverage = lerp(coverage, 0.87, 2.0);		  		  
	float density = 0.1 - 2.0 * 0.3;		  
	      rx = Coverage(coverage, density, rx);	
	float rh = smoothstep(0.999, 1.0, dot(-vp, -fs0(cd)));
	float4 rx2 = rx; 
	       rx2.a = min(1.0, rx2.a);  
	  float4 skyenv=tex2D(SamplerEnv, cd);		   
    float3 ssr = skyenv;	
	float4 rfl = reflection(n2.xyz, IN.txcoord.xy);		
           rfl = (rfl * 1.05); 
    float3 wp0 = wpd1(cd.xy);
    float3 p3 = float3(0.353, 0.353, 1.0);
           n2 = (normalize(n2.xyz*p3)*0.98);
    float3 nz = reflect(wp0.xyz, n2.xyz); 
    float3 rw = ((1000.0/0.01)*nz)/1000.0;	
    float3 ref11 = (wp0+rw);
    float2 rd = wpd2(ref11.xyz);		
	float  nf0 = saturate(8.0*(rd.y));
	       nf0 = pow(nf0, 1.5);
           ssr=lerp(ssr, rfl.xyz, rfl.w*nf0);
	float4 n1 = tex2D(SamplerNormal, IN.txcoord.xy);     
	float  rmix;		 
	       rmix=saturate(n1.z)*saturate(n1.z);
	       rmix*=rmix;					
		ssr.xyz=lerp(ssr, skyenv, rmix);
		ssr.xyz=lerp(ssr, r0*0.15, pow(rmix, 1.2));	
float wcr;
float wnr;
if (Wx.x==0,1) wcr = 0.0;
if (Wx.y==0,1) wnr = 0.0;
if (Wx.x==8)   wcr = 1.0;
if (Wx.x==16)  wcr = 1.0;
if (Wx.y==8)   wnr = 1.0;
if (Wx.y==16)  wnr = 1.0;	  
	float wmix = lerp(wcr, wnr, pow(Wx.z, 0.07));
	float4 nw = tex2D(SamplerNormal, IN.txcoord.xy).w;	  
	if (nw.w<1.0) nw = 0.0;
		  
	float3 n3 = CR(float3(0.0, 1.0, 1.0), wpos+CameraPosition);  		 
	float3 sv = normalize(float3(0.0, 0.0, 1.0));	 	
    float t1 = lerp(0.04, 0.04, x1);
          t1 = lerp(t1, 0.04, x2);
          t1 = lerp(t1, 0.09, x3);
          t1 = lerp(t1, 0.09, x4);
          t1 = lerp(t1, 0.09, xE);
          t1 = lerp(t1, 0.09, x5);
          t1 = lerp(t1, 0.09, x6);		 
          t1 = lerp(t1, 0.08, x7);
		  t1 = lerp(t1, 0.07, xG);
		  t1 = lerp(t1, 0.06, xZ);
          t1 = lerp(t1, 0.05, x8);  
          t1 = lerp(t1, 0.04, x9);	
          t1*= 1.1;		  
   float factor = t1 - dot(sv, n2);
         factor = pow(factor, 12.0);
   float fr = (factor*factor); 
         fr/= 4.5;	
	float n6 = saturate(n3);
	float lp = lerp(0.0, fr, n6);
	
	if(d1 < 3000)r0.xyz = lerp(r0, 0.75*ssr+lp, nw*rh*rx2*1.0);  
	float4 rp = float4(normalize(wpos.xyz), 1.0);
	float3 rpos = normalize(rp.xyz)*1.0;
	float3x3 vec = float3x3(1,0,0,0,cos(0.25),sin(0.25),0,-sin(0.25),cos(0.25));				   
		   rpos = mul(rpos, vec);	
	float2 f0=float2(acos(rpos.z),atan(rpos.x/rpos.y));		  		  
	float2 RainTimer = float2(Timer.x*31000.0, 0.0); 
	float4 raindx2 = Rain2(-f0.xy+RainTimer*0.7);
	float4 raindx1 = Rain1(-f0.xy+RainTimer*1.0);	  
    float4 raindx0 = Rain0(-f0.xy+RainTimer*2.9);				 
	float a0 = smoothstep(1.0, 0.90, abs(rpos.z));
    float pow2 = length(wpos.xyz);
    float distance = 0.0;	   
	   float3 rd0 = 1.0-1.0/exp(length(distance-pow2)/0.1);
	   float3 rd1 = 1.0-1.0/exp(length(distance-pow2)/8.0);
	   float3 rd2 = 1.0-1.0/exp(length(distance-pow2)/95.0);		   
	   float3 dr0 = step(0.15, rd0*raindx0);
	   float3 dr1 = step(0.1, rd1*raindx1);
	   float3 dr2 = step(0.1, rd2*raindx2);
	   float4   rn0 = tex2Dlod(SamplerColor, float4(IN.txcoord.xy+raindx2*float2(0.0, -0.10), 0.0, 4.0));	
	   		  	rn0*= 0.63; 	   
	   float4   rn1 = tex2Dlod(SamplerColor, float4(IN.txcoord.xy+raindx1*float2(0.0, -0.13), 0.0, 4.0));  
	   		  	rn1*= 0.63; 	   
	   float4   rn2 = tex2Dlod(SamplerColor, float4(IN.txcoord.xy+raindx0*float2(0.0, -0.15), 0.0, 4.0));
	   	   		rn2*= 0.63; 
	   		  	rn0.a = min(0.9, rn0.a); 
		  	    rn1.a = min(0.9, rn1.a);	   
		  	    rn2.a = min(0.9, rn2.a);
    float t2 = lerp(0.0, 0.0, x1);
          t2 = lerp(t2, 0.10, x2);
          t2 = lerp(t2, 0.20, x3);
          t2 = lerp(t2, 0.25, x4);
          t2 = lerp(t2, 0.25, xE);
          t2 = lerp(t2, 0.25, x5);
          t2 = lerp(t2, 0.25, x6);		 
          t2 = lerp(t2, 0.15, x7);
		  t2 = lerp(t2, 0.12, xG);
		  t2 = lerp(t2, 0.10, xZ);
          t2 = lerp(t2, 0.10, x8);  
          t2 = lerp(t2, 0.07, x9);						
	     r0.xyz = lerp(r0, 1.05*rn0+t2, wmix*dr2*a0*rn0.a);
		 r0.xyz = lerp(r0, 1.07*rn1+t2, wmix*dr1*a0*rn1.a);
		 r0.xyz = lerp(r0, 1.09*rn2+t2, wmix*dr0*a0*rn2.a);
float wc1;
float wn1;
if (Wx.x==0,1) wc1 = 0.0;
if (Wx.y==0,1) wn1 = 0.0;
if (Wx.x==8)   wc1 = 1.0;
if (Wx.y==8)   wn1 = 1.0;
if (Wx.x==16)  wc1 = 1.0;
if (Wx.y==16)  wn1 = 1.0;
float wmix2 = lerp(wc1, wn1, Wx.z);
float3 fx0 = FogFarColor;
	float3 st3 = normalize(fx0.xyz);
	float3 cs3=fx0.xyz/st3.xyz;
	       cs3=pow(cs3, 6.0);
	   fx0.xyz = cs3;	
float fx = saturate(fx0);
float3 npos0 = normalize(wpos.xyz);

   float vecx2 = 5.45;
   float vecx3 = 3.85; 
   float vecx4 = 1.65;	
   float3 ns0 = normalize(wpos.xzy);
   float3x3 vec0 = float3x3(1, 0, 0, 
                            0, cos(vecx2), sin(vecx2),
							0, -sin(vecx2), cos(vecx2));   
		   ns0 = mul(ns0, vec0);		   
	float3 np1 = normalize(ns0);
	
   float3 ns1 = normalize(wpos.xyz);
   float3 ns2 = normalize(wpos.xzy);   
   float3x3 vec1 = float3x3(1, 0, 0, 
                            0, cos(vecx3), sin(vecx3),
							0, -sin(vecx3), cos(vecx3));   
							
   float3x3 vec2 = float3x3(0, 0, 1, 
                            cos(vecx4), sin(vecx4), 0,
							-sin(vecx4), cos(vecx4), 0);   							
		   ns1 = mul(ns1, vec1);
		   ns2 = mul(ns2, vec2);
		   	   
	float3 np2 = normalize(ns1.xyz);	
	float3 np3 = normalize(ns2.xyz);
float4 fx2 = Lightning(np1*10.0)*3.0;
	   fx2*= smoothstep(0.0, 0.3, npos0.z);
float4 fx3 = Lightning(np2*10.0)*3.0;   
	   fx3*= smoothstep(0.0, 0.3, npos0.z);
float4 fx4 = Lightning2(np3*10.0, -7.9, -7.3)*3.0;   	
float4 fx5 = Lightning2(np3*10.0, 1.0, -6.4)*3.0;   	   
 
	  	r0.xyz*= lerp(1.0, 1.8, wmix2*fx);
float L = wpos.xyz;	  

	  float ti = max(sin(Timer.x*100000.0*0.5),0);
	  float ti2 = max(sin(Timer.x*100000.0*1.0),0);	  
	float3 mix0 = lerp(fx2, fx3, ti);  
	float3 mix1 = lerp(fx4, fx5, ti);  	
	
	float3 mix2 = lerp(mix0, mix1, ti2); 

	   if( 6000 < L || d0 == 1)
     {
		 r0.xyz =lerp(r0, r0+mix2, wmix2*fx);
      }	
	  
  return r0;
}



//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
technique PostProcess
{
   pass P0
   {
      VertexShader = compile vs_3_0 VS_PostProcess();
      PixelShader  = compile ps_3_0 PS_Lighting();
   }
}

technique PostProcess2
{
	pass P0
	{
      VertexShader = compile vs_3_0 VS_PostProcess();
      PixelShader  = compile ps_3_0 PS_Sky();
	}
}

technique PostProcess3
{
   pass P0
   {
      VertexShader = compile vs_3_0 VS_PostProcess();
      PixelShader  = compile ps_3_0 PS_Rain();
   }
}


